package com.shitou.web;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.shitou.pojo.Project;
import com.shitou.pojo.Report;
import com.shitou.pojo.User;
import com.shitou.service.ProjectService;
import com.shitou.service.UserService;

@Results({@Result(name=BaseAction.RELOAD,location="project.jspx",params={"id","${id}"},type="redirectAction")})
public class ProjectAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	public static final String MAIN = "main";
	public static final String REPORT = "report";
	@Resource
	private ProjectService projectService;
	@Resource
	private UserService userService;

	private List<User> userList;
	private List<Project> projectList;
	private List<Report> reportList;
	private Project project;
	private String name;
	private String users;
	private Integer id;

	public String input() throws Exception {
		userList = userService.findAll();
		return INPUT;
	}
	
	@Override
	public String del() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		if(id == null || id == 0) {
			id =  projectService.findLast().getId();
		}
		project = projectService.findById(id);
		return MAIN;
	}

	@Override
	public String save() throws Exception {
		id = projectService.save(name, users,(User)getSessionAttribute("user"));
		return RELOAD;
	}
	
	public String report() throws Exception {
		projectList = projectService.getReport(getRealPath());
		return REPORT;
	}
	
	public String report2() throws Exception {
		reportList = projectService.getReport();
		return REPORT;
	}
	
	//ajax方法
	public String ajaxlist() throws Exception {
		//List<Project> projectList = projectService.findAll();
		List<Project> projectList = projectService.findByUser((User)getSessionAttribute("user")); 
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[]{"users","milestones","documents","issueses"});
		JSONArray json = JSONArray.fromObject(projectList,config);
		renderJson(json.toString());
		return null;
	}
	
	public String ajaxuserlist() throws Exception {
		Project p = projectService.findById(id);
		List<User> userList = p.getUsers();
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[]{"projects","assignments","issueses","groupList"});
		JSONArray json = JSONArray.fromObject(userList,config);
		renderJson(json.toString());
		return null;
	}

	//getter and setter
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUsers() {
		return users;
	}

	public void setUsers(String users) {
		this.users = users;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<Project> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}

	public List<Report> getReportList() {
		return reportList;
	}

	public void setReportList(List<Report> reportList) {
		this.reportList = reportList;
	}
}
